package cn.lopr.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author shuangyang7
 * @date 2021/1/3 17:01
 */
public class DateUtils {

    public static SimpleDateFormat hmsFormat = new SimpleDateFormat("HH:mm:ss");
    public static SimpleDateFormat dmyFormat = new SimpleDateFormat("dd/MM/yy");
    public static SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy/MM/dd");
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");

    public static String getTime() {
        //设置时区，如：+8:00 或者取时区id(GMT)
        TimeZone time = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(time);

        Calendar calendar = Calendar.getInstance();
        //获取Date对象
        Date date = calendar.getTime();
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return format.format(date);
    }

    public static Date getDateTime() {
        return null;
    }

    /**
     * @return String res
     */
    public static String getTimeDesc(Date date) {
        //设置时区，如：+8:00 或者取时区id(GMT)
        TimeZone time = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        LocalDateTime localDateTime = LocalDateTime.of(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) + 1, calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
        LocalDateTime now = LocalDateTime.now();
        Duration between = Duration.between(localDateTime, LocalDateTime.now());
        long l = between.toMinutes();
        System.out.println(localDateTime.getMinute() + "  " + now.getMinute() + " " + l);
        if (l <= 5) {
            return "刚刚";
        }

        if (l <= 60) {
            return l + "分钟之前";
        }

        if (l <= 1440) {
            return l % 60 + "小时之前";
        }
        return l % 1440 + "天之前";
    }

    public static void main(String[] args) {

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, 4);
        System.out.println(getTimeDesc(calendar.getTime()));

        System.out.println(DateUtils.parseYMDHMS("2022-12-12 12:12:12"));
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }

    public static Date getTimeIsDateType() {
        //设置时区，如：+8:00 或者取时区id(GMT)
        TimeZone time = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(time);

        Calendar calendar = Calendar.getInstance();
        //获取Date对象
        return calendar.getTime();
    }

    public static String formatYMD(Date date) {
        return ymdFormat.format(date);
    }

    public static String formatYMD(String date) {
        try {
            return formatYMD(format.parse(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseYMD(String date) {
        try {
            return ymdFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static Date parseYMDHMS(String date) {
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            try {
                return formatYMD.parse(date);
            } catch (ParseException parseException) {
                parseException.printStackTrace();
            }
        }
        return null;
    }

    public static String formatDMY(Date date) {
        return dmyFormat.format(date);
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String formatDMY(String date) {
        try {
            return formatDMY(format.parse(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String formatHMS(Date date) {
        return hmsFormat.format(date);
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String formatHMS(String date) {
        try {
            return formatHMS(format.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date formatYMDHMS(String date) {
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String formatYMDHMS(Date date) {
        return format.format(date);
    }

    //public static boolean isCurrentDay(String date) {
    //
    //    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    //    try {
    //        return DateUtils.isToday(Objects.requireNonNull(sdf.parse(date)).getTime());
    //    } catch (ParseException e) {
    //        e.printStackTrace();
    //    }
    //    return false;
    //}
}
